/// Splits a stream of consecutive strings into lines.
///
/// The input string is provided in smaller chunks through
/// the `source` stream.
Stream<String> lines(Stream<String> source) async* {
  // Stores any partial line from the previous chunk.
  var partial = '';
  // Wait until a new chunk is available, then process it.
  await for (final chunk in source) {
    var lines = chunk.split('\n');
    lines[0] = partial + lines[0]; // Prepend partial line.
    partial = lines.removeLast(); // Remove new partial line.
    for (final line in lines) {
      yield line; // Add lines to output stream.
    }
  }
  // Add final partial line to output stream, if any.
  if (partial.isNotEmpty) yield partial;
}
void myPrint(String str){
  print('myPrint:$str');
}


Stream<int> timedCounter(Duration interval, [int? maxCount]) async* {
  int i = 0;
  while (true) {
    await Future.delayed(interval);
    yield i++;
    if (i == maxCount) break;
  }
}
void main()async {
  myPrint('Start');
  var counterStream = Stream<int>.periodic(
    const Duration(seconds: 1),
        (x) => x,
  );
  counterStream.listen((event){
    print('event:$event');
  });
  // counterStream.forEach(print); // Print an integer every second, 15 times.
  // var stream = timedCounter(const Duration(seconds: 1),10);
  // stream.forEach(print);
  await Future.delayed(const Duration(seconds: 20));
  print('End');
}